<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog title="新增权限点" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="enterFn">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 是否显示弹层
    showDialog: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validName = (rule, value, callback) => {
      // console.log(this.clickId)
      let newList = this.perList.filter(item => item.pid === this.clickId && item.id !== this.clickId).map(item => item.name.trim())
      // console.log(newList)
      const pid = this.perList.filter(item => item.id === this.clickId).map(item => item.pid)[0]
      // console.log(pid)
      if (this.isEdit) {
        newList = this.perList.filter(item => item.pid === pid && item.id !== this.clickId).map(item => item.name.trim())
      }
      return newList.includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    const validCode = (rule, value, callback) => {
      let newList = this.perList.map(item => item.code)
      if (this.isEdit) {
        newList = this.perList.filter(item => item.id !== this.clickId).map(item => item.code)
      }
      return newList.includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    return {
      permissionList: [], // 权限管理列表数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0' // 开启
      },
      // 扁平数据
      perList: [],
      clickId: '',
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }, { validator: validName, trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }, { validator: validCode, trigger: 'blur' }]
      }
    }
  }, methods: {
    // 关闭弹框
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.perForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 确定按钮
    enterFn() {
      this.$refs.perForm.validate(valid => {
        if (!valid) return
        this.$emit('confirmEV', { ...this.formData })
        this.btnCancel()
      })
    }
  }
}
</script>

<style lang='scss' scoped>

</style>
